import { Component } from '@angular/core';
import {App, IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {SystemMessageConfirmPage} from "../system-message-confirm/system-message-confirm";
import {USER} from "../../service/user.service";
import {USERAPI} from "../../api/user.api.service";
import {PageParam} from "../../api/api.service";
import moment from "moment"
import {LocalStorage} from "../../utils/localstorage.utils";


/**
 * Generated class for the SystemMessagePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-system-message',
  templateUrl: 'system-message.html',
})
export class SystemMessagePage {
  pageParam:PageParam;
  hasMore:boolean = true;
  data={};

  messages = [
  ];

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public app:App,
              public USER:USER,
              public ls:LocalStorage,
              public USERAPI:USERAPI,
              private modalCtrl: ModalController
  ) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SystemMessagePage');
  }

  submit(item){
    this.ls.setObject('cmessage',item);
    let profileModal = this.modalCtrl.create(SystemMessageConfirmPage, {}, {showBackdrop: true});
    profileModal.present();
  }
  ionViewDidEnter() {
    document.title='系统消息';
    this.doRefresh()
  }
  doRefresh(refresher?) {
    this.pageParam = new PageParam()
    this.USERAPI.fetchMessage(v => {
      this.messages = v.content
      console.log(this.messages);
      this.hasMore = !v.last;
      if (refresher) {
        refresher.complete();
      }
    }, this.pageParam);
  }

  formatDate(date):string {
    return moment(date).format('YYYY-MM-DD')
  }

  get canLoadMore():boolean {
    return this.hasMore;
  }

  doInfinite(infiniteScroll) {
    ++this.pageParam.page;
    this.USERAPI.fetchMessage(v => {
      this.hasMore = !v.last;
      this.messages = this.messages.concat(v.content);
      infiniteScroll.complete();
    }, this.pageParam);
  }

}
